﻿using Kingdee.BOS.Orm.DataEntity;
using SD.WRZS.K3.ServiceFacade.KDServiceEntity.Mobile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDWXServer.Models
{
    /// <summary>
    /// 销售合同
    /// </summary>
    [Serializable]
    public class WXSaleContract : AbstractBillObject
    {
        public WXSaleContract()
        {
            BillTypeName = "销售合同";
            Organization = new Organization();
            DocumentStatusName = "";
            DeliveryDate = DateTime.Now.Date;
            Customer = new Customer();
            Rows = new List<WXSaleContractRow>();
        }

        /// <summary>
        /// 单据状态名称
        /// </summary>
        public string DocumentStatusName { get; set; }
        /// <summary>
        /// 提货地点
        /// </summary>
        public Organization Organization { get; set; }

        /// <summary>
        /// 交货日期
        /// </summary>
        public DateTime DeliveryDate { get; set; }
        /// <summary>
        /// 客户
        /// </summary>
        public Customer Customer { get; set; }

        public List<WXSaleContractRow> Rows { get; set; }
        public static WXSaleContract Parse(DynamicObject obj)
        {
            var item = new WXSaleContract();
            item.Id = Convert.ToInt64(obj["Id"]);
            item.BillNo = Convert.ToString(obj["BILLNO"]);
            item.BillDate = Convert.ToDateTime(obj["FDate"]);
            item.DocumentStatus = Convert.ToString(obj["FDOCUMENTSTATUS"]);
            item.CloseStatus = Convert.ToString(obj["FCLOSESTATUS"]);
            switch (item.DocumentStatus)
            {
                case "Z":
                    item.DocumentStatusName = "暂存";
                    break;
                case "A":
                    item.DocumentStatusName = "创建";
                    break;
                case "B":
                    item.DocumentStatusName = "审核中";
                    break;
                case "C":
                    item.DocumentStatusName = "已审核";
                    break;
                case "D":
                    item.DocumentStatusName = "重新审核";
                    break;
            }
            if (item.CloseStatus == "B")
            {
                item.DocumentStatusName = "已关闭";
            }
            item.DeliveryDate = Convert.ToDateTime(obj["FVALIENDDATE"]).AddYears(-1);

            var orgObj = obj["FSALEORGID"] as DynamicObject;
            item.Organization.Id = Convert.ToInt64(orgObj["Id"]);
            item.Organization.Number = Convert.ToString(orgObj["Number"]);
            item.Organization.Name = Convert.ToString(orgObj["Name"]);

            var cust = obj["FBDCUSTID"] as DynamicObject;
            item.Customer.Id = Convert.ToInt64(cust["Id"]);
            item.Customer.MasterId = Convert.ToInt64(cust["msterID"]);
            item.Customer.Number = Convert.ToString(cust["Number"]);
            item.Customer.Name = Convert.ToString(cust["Name"]);

            var entryRows = obj["FCRMContractEntry"] as DynamicObjectCollection;
            foreach (var row in entryRows)
            {
                var subItem = new WXSaleContractRow();
                item.Rows.Add(subItem);

                subItem.Id = Convert.ToInt64(row["Id"]);
                var material = row["MaterialId"] as DynamicObject;
                subItem.Material.Id = Convert.ToInt64(material["Id"]);
                subItem.Material.MasterId = Convert.ToInt64(material["msterID"]);
                subItem.Material.Number = Convert.ToString(material["Number"]);
                subItem.Material.Name = Convert.ToString(material["Name"]);
                subItem.Material.Specification = Convert.ToString(material["Specification"]);

                var unit = row["UnitId"] as DynamicObject;
                subItem.Material.StockUnit.Id = Convert.ToInt64(unit["Id"]);
                subItem.Material.StockUnit.Number = Convert.ToString(unit["Number"]);
                subItem.Material.StockUnit.Name = Convert.ToString(unit["Name"]);

                subItem.Material.SaleUnit.Id = subItem.Material.StockUnit.Id;
                subItem.Material.SaleUnit.Number = subItem.Material.StockUnit.Number;
                subItem.Material.SaleUnit.Name = subItem.Material.StockUnit.Name;

                subItem.Material.PurchaseUnit.Id = subItem.Material.StockUnit.Id;
                subItem.Material.PurchaseUnit.Number = subItem.Material.StockUnit.Number;
                subItem.Material.PurchaseUnit.Name = subItem.Material.StockUnit.Name;

                subItem.PlanQty = Convert.ToDecimal(row["Qty"]);
                subItem.TaxPrice = Convert.ToDecimal(row["TaxPrice"]);
                subItem.AllAmount = Convert.ToDecimal(row["AllAmount"]);
            }

            return item;
        }
    }
    /// <summary>
    /// 销售合同行
    /// </summary>
    [Serializable]
    public class WXSaleContractRow
    {
        public WXSaleContractRow()
        {
            Material = new Material();
        }
        /// <summary>
        /// 行内码
        /// </summary>
        public Int64 Id { get; set; }
        /// <summary>
        /// 物料
        /// </summary>
        public Material Material { get; set; }
        /// <summary>
        /// 计划数量
        /// </summary>
        public decimal PlanQty { get; set; }
        /// <summary>
        /// 出库数量
        /// </summary>
        public decimal StockQty { get; set; }
        /// <summary>
        /// 累计提货数量
        /// </summary>
        public decimal DeliQty { get; set; }
        /// <summary>
        /// 含税单价
        /// </summary>
        public decimal TaxPrice { get; set; }
        /// <summary>
        /// 价税合计
        /// </summary>
        public decimal AllAmount { get; set; }
    }
}